package com.coocaa.ops.admin.v1.auth.sys;

import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.annotation.Config;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.auth.sys.constant.MenuTokenConstants;
import com.coocaa.ops.admin.v1.auth.sys.model.query.SysDeptModel;
import com.coocaa.ops.admin.v1.auth.sys.service.impl.SysDeptServiceImpl;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.auth.entity.SysDeptEntity;
import com.coocaa.ops.common.tool.json.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2019/3/13.
 * @description
 */
@RestController
@Config(menuToken = MenuTokenConstants.SYS_DEPT)
@RequestMapping("/v1/sysDept")
public class SysDeptController extends MgrBaseController {
    @Autowired
    private SysDeptServiceImpl sysDeptService;
    private static CommonLanguageProperties commonLanguageProperties;

    /**
     * 保存
     *
     * @param request
     * @param jsonStr
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(HttpServletRequest request, @RequestParam String jsonStr) {
        SysDeptEntity entity = (SysDeptEntity) JsonUtil.json2Object(jsonStr, SysDeptEntity.class);
        sysDeptService.save(entity);
        return R.ok();
    }

    /**
     * 获取详情信息
     *
     * @param id
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getDetailInfo", method = {RequestMethod.GET})
    public Object getDetailInfo(Integer id) {
        InputParamsValidator.validIdValue(id);
        SysDeptEntity entity = sysDeptService.getDetailInfo(id);
        Map result = R.ok(entity);
        return render2Json(result);
    }


    /**
     * 分页数据
     *
     * @param request
     * @param model
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/pageList", method = {RequestMethod.GET, RequestMethod.POST})
    public Object pageList(HttpServletRequest request, SysDeptModel model) {
        Map<String, Object> data = sysDeptService.getPageResult(model);
        return render2Json(data);
    }

    /**
     * 获取所有的部门
     *
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/listAllDept", method = {RequestMethod.GET, RequestMethod.POST})
    public Object listAllDept(HttpServletRequest request) {
        Map<String, Object> data = sysDeptService.listAllDept();
        return render2Json(data);
    }


    /**
     * 更新排序
     *
     * @param request
     * @param id
     * @param seq
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.EDIT)
    @RequestMapping(value = "/updateSeq", method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public Object updateSeq(HttpServletRequest request, Integer id, Integer seq) {
        sysDeptService.updateSeq(id, seq);
        return R.ok().msg(commonLanguageProperties.getUpdateSuccess());
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        SysDeptController.commonLanguageProperties = commonLanguageProperties;
    }

    /**
     * 删除记录
     *
     * @param
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.DELETE)
    @RequestMapping(value = "/remove", method = {RequestMethod.GET, RequestMethod.POST})
    public Object remove(@RequestParam Integer[] deptId, HttpServletRequest request) {
        return sysDeptService.deleteBatch(deptId, WebUtil.getCurrentUser(request));
    }

    /**
     * 获取菜单运行权限列表
     *
     * @param request
     * @param
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getMenuInfo", method = {RequestMethod.GET}, produces = "text/html;charset=UTF-8")
    public Object getMenuInfo(HttpServletRequest request) {
        Map result = getMenuInfo(this.getClass(), request);
        return render2Json(result);
    }

}
